package com.fltry.zillionaire

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import java.util.*

class DishView : View {
    private var dishItems = mutableListOf<DishItem>()
    private var diameter: Int = 0
    private var radius: Float = 0f
    private lateinit var mPaint: Paint
    private var angle = 0f

    private var isTurn = false
    private var allTime = 0

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView()
    }

    private fun initView() {
        mPaint = Paint()
        mPaint.isAntiAlias = true
        mPaint.color = Color.RED
        mPaint.textSize = 40f

        dishItems.add(DishItem(1, "汉堡"))
        dishItems.add(DishItem(2, "可乐"))
        dishItems.add(DishItem(3, "炸鸡"))
        dishItems.add(DishItem(4, "薯条"))
        dishItems.add(DishItem(5, "牛奶"))
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val mWidth = measuredWidth
        val mHeight = measuredHeight
        diameter = Math.min(mWidth, mHeight)
        radius = (diameter / 2).toFloat()
        setMeasuredDimension(diameter, diameter)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        drawPoint(canvas)
        drawCircle(canvas)
        drawCircleButton(canvas)
        drawLine(canvas)
    }

    private fun drawCircleButton(canvas: Canvas?) {
        mPaint.strokeWidth = 1f
        mPaint.style = Paint.Style.FILL
        canvas!!.drawCircle(radius, radius, radius / 5, mPaint)
    }

    private fun drawPoint(canvas: Canvas?) {
        mPaint.strokeWidth = 10f
        mPaint.style = Paint.Style.STROKE
        canvas!!.drawLine(radius, radius / 3, radius, radius, mPaint)
    }

    private fun drawLine(canvas: Canvas?) {
        mPaint.strokeWidth = 1f
        mPaint.style = Paint.Style.FILL
        canvas!!.rotate(angle, radius, radius)
        val rotateRadius = 360 / dishItems.size
        for (i in 0 until dishItems.size) {
            canvas.rotate(rotateRadius.toFloat(), radius, radius)
            canvas.drawLine(0f, radius, radius, radius, mPaint)
            canvas.drawText(dishItems[i].name, radius / 2, radius / 2, mPaint)
        }
    }

    private fun drawCircle(canvas: Canvas?) {
        mPaint.strokeWidth = 1f
        mPaint.style = Paint.Style.STROKE
        canvas!!.drawCircle(radius, radius, radius, mPaint)
    }


    fun start() {
        mHandler.postDelayed({
            if (angle < allTime && isTurn) {
                angle += 10
                mHandler.sendEmptyMessage(1)
            } else {
                mHandler.sendEmptyMessage(2)
            }
        }, 10)
    }


    private var mHandler: Handler = @SuppressLint("HandlerLeak")
    object : Handler() {
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            when (msg?.what) {
                1 -> {
                    invalidate()
                    start()
                }
                2 -> {
                    isTurn = false
                    angle = 0f
                }
            }
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event!!.action) {
            MotionEvent.ACTION_DOWN -> {
                val eventX = event.x
                val eventY = event.y
                if (eventX >= radius * 4 / 5
                        && eventX <= radius * 6 / 5
                        && eventY >= radius * 4 / 5
                        && eventY <= radius * 6 / 5
                        && !isTurn) {
                    isTurn = true
                    allTime = 100 * Random().nextInt(10) * 10
                    start()
                }
            }
        }
        return super.onTouchEvent(event)
    }
}
